<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Logic\EmailLogic;
use App\Model\OAEmail;
use App\Model\OAStaff;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * @desc 邮箱
 */
class EmailController extends Controller
{
    public function getList(Request $request)
    {
        try{

            //dd(DB::select("select version();"));
            $this->checkHeader($request->header());
            DB::enableQueryLog();
            $list = OAEmail::getEmailList($request->header('uid'));
            if(empty($list)) ret_json('ok','ok',$list);
            foreach ($list as &$val){
                $val['userFrom'] = OAStaff::query()->where('id',$val['uid_from'])->value('username');
                $val['desc'] = mb_substr($val['desc'],0,10);
            }
            ret_json('ok','ok',$list);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    public function sendEmail(Request $request){
        try{
            $rules = [
                'desc'=>'required:string',
                'uid_to'=>'required:exists:staff,id',
                'file_path'=>'sometimes:string',
            ];
            $this->checkHeader($request->header());
            $data = $this->mergeRequest($request);
            $this->verify($data,$rules);
            $result = (new EmailLogic())->sendEmail($data);
            ret_json('ok','发送成功',$data);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }

    }

    //获取聊天内容
    public function getChartContent(Request $request)
    {
        try{
            $rules = [
                'uid_from'=>'numeric|required',
            ];
            $this->checkHeader($request->header());
            $params = $this->mergeRequest($request);
            $this->verify($params,$rules);
            $list = (new EmailLogic())->getChartContent($params);
            ret_json('ok','ok',$list);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    public function searchUser(Request $request)
    {
        try{
            $rules = [
                'username'=>'string|required',
            ];

            $this->checkHeader($request->header());
            $params = $this->mergeRequest($request);
            $this->verify($params,$rules);
            $list = (new EmailLogic())->searchUser($params);
            ret_json('ok','ok',$list);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }
}
